//
//  DetailMyBoardCell.m
//  Bespoke
//
//  Created by Cheng on 12/7/24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DetailMyBoardCell.h"
#import "MediaListsMedia.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "Split.h"
#import "LoadingUI.h"

#define BoarderHight 100
#define TEXT_HEIGHT 39

@implementation DetailMyBoardCell
@synthesize titleLabel, imageView, IVw_logo, IVw_lines, ActivityView;

- (void) updateDetailCellInfo:(MediaListsMedia *)Media
                     MaxWidth:(double)MaxWidth
                   FullScreen:(BOOL)FullScreen {
    
    self.IVw_logo.image = [[UIImage alloc] initWithContentsOfFile:[LoadingUI IconPressed:Media.NS_Source]];
    
    
    if((Media.imageArray.count != 0)||(Media.MLH_html.NS_ImageName!=nil)) {
        
        NSURL *url;
        
        if(Media.imageArray.count != 0) {
            
            self.titleLabel.text = Media.text.text_title;
            MediaListsImage *MediaListsImage = [Media.imageArray objectAtIndex:0];
            
            //NSLog(@"photoLoaderDidLoad: self.frame %f",self.imageView.image.size.width);
            Media.Width = MediaListsImage.Width;
            Media.Height = MediaListsImage.Height;
            url = [NSURL URLWithString:[NSString stringWithString:MediaListsImage.image_filename]];
        }
        else if (Media.MLH_html.NS_ImageName!=nil) {
            
            self.titleLabel.text = Media.MLH_html.NS_htmlTitle;
            
            //NSLog(@"photoLoaderDidLoad: self.frame %f",self.imageView.image.size.width);
            Media.Width = Media.MLH_html.ImageWidth;
            Media.Height = Media.MLH_html.ImageHeight;
            url = [NSURL URLWithString:[NSString stringWithString:Media.MLH_html.NS_ImageName]];
        }
        
        while (Media.Width > MaxWidth/2) {
            Media.Width = Media.Width/2;
            Media.Height = Media.Height/2+BoarderHight;
        }
        
        ActivityView.center = CGPointMake(Media.Width/2, Media.Height/2);
        
        [ActivityView startAnimating];
        
        __weak DetailMyBoardCell *Temp_self = self;
        
        [imageView setImageWithURL:url success:^(UIImage *image) {
            [Temp_self.ActivityView stopAnimating];[Temp_self.ActivityView removeFromSuperview];
        } failure:^(NSError *error) {
            [Temp_self.ActivityView stopAnimating];[Temp_self.ActivityView removeFromSuperview];
        }];
        
        if(FullScreen) {
            [self.imageView setFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];
            titleLabel.hidden = TRUE;
            IVw_logo.hidden = TRUE;
            IVw_lines.hidden = TRUE;
        }
        else {
            [self.imageView setFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height-TEXT_HEIGHT)];
            titleLabel.hidden = FALSE;
            IVw_logo.hidden = FALSE;
            IVw_lines.hidden = FALSE;
        }

        self.imageView.clipsToBounds = YES;
    }
    else {
        [self.ActivityView stopAnimating];
        [self.ActivityView removeFromSuperview];
        
        Media.Width = self.frame.size.width;
        Media.Height = self.frame.size.height-self.imageView.frame.size.height;
    }
    
    //Check Title display
    if((![Media.text.text_title isEqualToString:@""])&&(Media.text.text_title!=nil))
        self.titleLabel.text = [Media.text.text_title TitleSplitPressed];
    else if((![Media.MLH_html.NS_htmlTitle isEqualToString:@""])&&(Media.MLH_html.NS_htmlTitle!=nil))
        self.titleLabel.text = [Media.MLH_html.NS_htmlTitle TitleSplitPressed];
    else if((![Media.text.text_description isEqualToString:@""])&&(Media.text.text_description!=nil))
        self.titleLabel.text = [Media.text.text_description TitleSplitPressed];
    
    if(([self.titleLabel.text isEqualToString:@""])||(self.titleLabel.text==nil)) {
        self.titleLabel.text = @"  ";
        titleLabel.textColor = [UIColor whiteColor];
    }
}

@end
